﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceAgent.PortfolioService;
using System.ServiceModel;
using System.Collections.ObjectModel;

namespace ServiceAgent
{
    public class PortfolioServiceAgent : IDisposable
    {
        PortfolioServiceClient proxy; 

        public PortfolioServiceAgent()
        {
            proxy = new PortfolioServiceClient();
        }

        #region LoaiHoSo
        public ObservableCollection<LoaiHoSoDTO> GetLoaiHoSoList()
        {
            try
            {
                proxy.Open();
                ObservableCollection<LoaiHoSoDTO> list =
                    proxy.GetLoaiHoSoList();
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<LoaiHoSoDTO> GetLoaiHoSoByDonViID(int DonViIDFilter)
        {
            try
            {
                proxy.Open();
                ObservableCollection<LoaiHoSoDTO> list =
                                    proxy.GetLoaiHoSoByDonViID(DonViIDFilter);
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<LoaiHoSoDTO> GetLoaiHoSoByNotYetDonViID(int DonViIDFilter)
        {
            try
            {
                proxy.Open();
                ObservableCollection<LoaiHoSoDTO> list =
                                    proxy.GetLoaiHoSoByNotYetDonViID(DonViIDFilter);
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public LoaiHoSoDTO GetLoaiHoSoByID(int IDFilter)
        {
            try
            {
                proxy.Open();
                LoaiHoSoDTO obj =
                    proxy.GetLoaiHoSoByID(IDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public LoaiHoSoDTO GetLoaiHoSoByTen(string TenFilter)
        {
            try
            {
                proxy.Open();
                LoaiHoSoDTO obj =
                    proxy.GetLoaiHoSoByTen(TenFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SaveLoaiHoSo(LoaiHoSoDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.SaveLoaiHoSo(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SaveLoaiHoSoList(ObservableCollection<LoaiHoSoDTO> dtolist)
        {
            try
            {
                proxy.Open();
                proxy.SaveLoaiHoSoList(dtolist);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemoveLoaiHoSo(LoaiHoSoDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.RemoveLoaiHoSo(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemoveLoaiHoSo(int IDFilter)
        {
            try
            {
                proxy.Open();
                proxy.RemoveLoaiHoSoByID(IDFilter);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SetLoaiHoSoForDonVi(int DonViIDFilter, int LoaiHoSoIDFilter)
        {
            try
            {
                proxy.Open();
                proxy.SetLoaiHoSoForDonVi(DonViIDFilter, LoaiHoSoIDFilter);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SetLoaiHoSoListForDonVi(int DonViIDFilter, ObservableCollection<LoaiHoSoDTO> listDTO)
        {
            try
            {
                proxy.Open();
                proxy.SetLoaiHoSoListForDonVi(DonViIDFilter, listDTO);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void UnSetLoaiHoSoListForDonVi(int DonViID, ObservableCollection<LoaiHoSoDTO> listDTO)
        {
            try
            {
                proxy.Open();
                proxy.UnSetLoaiHoSoListForDonVi(DonViID, listDTO);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void UnsetLoaiHoSoForDonVi(int DonViIDFilter, int LoaiHoSoIDFilter)
        {
            try
            {
                proxy.Open();
                proxy.UnsetLoaiHoSoForDonVi(DonViIDFilter, LoaiHoSoIDFilter);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        #endregion
         
        #region PhienBanGiao
        public ObservableCollection<PhienBanGiaoDTO> GetPhienBanGiaoList()
        {
            try
            {
                proxy.Open();
                ObservableCollection<PhienBanGiaoDTO> list =
                    proxy.GetPhienBanGiaoList();
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public PhienBanGiaoDTO GetPhienBanGiaoByDonViID(int DonViIDFilter)
        {
            try
            {
                proxy.Open();
                PhienBanGiaoDTO obj =
                    proxy.GetPhienBanGiaoByID(DonViIDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public PhienBanGiaoDTO GetPhienBanGiaoByID(int IDFilter)
        {
            try
            {
                proxy.Open();
                PhienBanGiaoDTO obj =
                    proxy.GetPhienBanGiaoByID(IDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public PhienBanGiaoDTO GetPhienBanGiaoByTen(string TenFilter)
        {
            try
            {
                proxy.Open();
                PhienBanGiaoDTO obj =
                    proxy.GetPhienBanGiaoByTen(TenFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SavePhienBanGiao(PhienBanGiaoDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.SavePhienBanGiao(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SavePhienBanGiaoList(ObservableCollection<PhienBanGiaoDTO> dtolist)
        {
            try
            {
                proxy.Open();
                proxy.SavePhienBanGiaoList(dtolist);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemovePhienBanGiao(PhienBanGiaoDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.RemovePhienBanGiao(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemovePhienBanGiao(int IDFilter)
        {
            try
            {
                proxy.Open();
                proxy.RemovePhienBanGiaoByID(IDFilter);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }
        #endregion

        #region HoSoDaNop
        public ObservableCollection<HoSoDaNopDTO> GetHoSoDaNopList()
        {
            try
            {
                proxy.Open();
                ObservableCollection<HoSoDaNopDTO> list =
                    proxy.GetHoSoDaNopList();
                proxy.Close();
                return list;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<HoSoDaNopDTO> GetHoSoDaNopBySinhVienID(int SinhVienIDFilter)
        {
            try
            {
                proxy.Open();
                ObservableCollection<HoSoDaNopDTO> obj =
                    proxy.GetHoSoDaNopBySinhVienID(SinhVienIDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public ObservableCollection<HoSoDaNopDTO> GetHoSoDaNopByLoaiHoSoID(int LoaiHoSoIDFilter)
        {
            try
            {
                proxy.Open();
                ObservableCollection<HoSoDaNopDTO> obj =
                    proxy.GetHoSoDaNopByLoaiHoSoID(LoaiHoSoIDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public HoSoDaNopDTO GetHoSoDaNopByID(int IDFilter)
        {
            try
            {
                proxy.Open();
                HoSoDaNopDTO obj =
                    proxy.GetHoSoDaNopByID(IDFilter);
                proxy.Close();
                return obj;
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SaveHoSoDaNop(HoSoDaNopDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.SaveHoSoDaNop(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void SaveHoSoDaNopList(ObservableCollection<HoSoDaNopDTO> dtolist)
        {
            try
            {
                proxy.Open();
                proxy.SaveHoSoDaNopList(dtolist);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemoveHoSoDaNop(HoSoDaNopDTO objdto)
        {
            try
            {
                proxy.Open();
                proxy.RemoveHoSoDaNop(objdto);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }

        public void RemoveHoSoDaNop(int IDFilter)
        {
            try
            {
                proxy.Open();
                proxy.RemoveHoSoDaNopByID(IDFilter);
                proxy.Close();
            }
            catch (Exception) { throw; }
            finally
            {
                if (proxy.State == CommunicationState.Opened)
                    proxy.Abort();
            }
        }
        #endregion

        #region Disposable
        public void Dispose()
        {
            this.DisposeExistingServiceClientIfRequired();
        }

        private void DisposeExistingServiceClientIfRequired()
        {
            if (proxy != null)
            {
                try
                {
                    if (proxy.State == CommunicationState.Faulted)
                    {
                        proxy.Abort();
                    }
                    else
                    {
                        proxy.Close();
                    }
                }
                catch
                {
                    proxy.Abort();
                }

                proxy = null;
            }
        }
        #endregion
    }
}
